package com.jcxy.controller;

import com.jcxy.VO.AttendanceVo;
import com.jcxy.VO.UserVo;
import com.jcxy.mapper.AttendanceMapper;
import com.jcxy.pojo.Position;
import com.jcxy.pojo.User;
import com.jcxy.service.AttendanceService;
import com.jcxy.util.PageBean;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/attendanceManage")
public class AttendanceManageController {

    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private AttendanceMapper attendanceMapper;

    /**
     * 通过分页获得所有用户列表
     * @return
     */
    @RequestMapping("/getAttendanceListByPage.do")
    @ResponseBody
    public ResponseData getAttendanceListByPage(HttpServletRequest request){


        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());

        //调用业务逻辑层方法，查询角色列表
        PageBean pageBean = attendanceService.queryListByPage(
                dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 跳转到userEdit页面
     * @param request
     * @return
     */
    @RequestMapping("/toAttendanceEditPage.do")
    public String toAttendanceEditPage(HttpServletRequest request){

        //获取用户id
        String attendance_id = request.getParameter("attendance_id");
        //获取用户信息
        AttendanceVo attendanceVo = attendanceMapper.queryAttendanceById(attendance_id);

        //将考勤信息存储在请求中
        request.setAttribute("attendanceVo",attendanceVo);

        return "attendanceManage/attendanceEdit";
    }

    /**
     * 修改考勤
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateAttendance.do")
    public String updateAttendance(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        attendanceService.updateAttendance(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "attendanceManage/attendanceManage";
    }

    /**
     * 删除考勤
     * @param attendance_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteAttendanceById.do")
    @ResponseBody
    public ResponseData deleteAttendanceById(Integer attendance_id) throws Exception{

        attendanceService.deleteAttendance(attendance_id);

        return ResponseData.ok();
    }

}
